<script setup>
import PageContainer from '@/components/PageContainer.vue' // 引入页面容器
import ChannelEdit from '@/components/ChannelEdit.vue'
import { Delete, Edit } from '@element-plus/icons-vue' // 引入图标
import { cateListService, cateDeleteService } from '@/api/article' // 引入api
import { ref, onMounted } from 'vue' // 引入ref 和 onMounted
const channelList = ref([]) // 创建ref文章分类列表
// 挂载到页面容器时获取文章分类列表
onMounted(async () => {
  getChannelList()
})

const loading = ref(false) // 创建loading v-loading="loading"  挂载到 table 组件
const dialog = ref() // 创建ref获取弹窗 这样就可以调用 dialog.value.open()
// 获取文章分类列表
const getChannelList = async () => {
  loading.value = true
  const res = await cateListService()
  channelList.value = res.data
  loading.value = false
}
// 编辑分类
const onEditChannel = async (row) => {
  dialog.value.open(row)
}
// 删除分类
const onDelChannel = async (row) => {
  await ElMessageBox.confirm('你确认删除该分类信息吗？', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })

  await cateDeleteService(row.id)

  await getChannelList()
}
// 添加分类
const onAddChannel = () => {
  dialog.value.open({})
}
// 成功回调
const onSuccess = () => {
  getChannelList()
}
</script>
<template>
  <page-container title="文章分类">
    <template #extra>
      <el-button type="primary" @click="onAddChannel"> 添加分类 </el-button>
    </template>
    <!-- 表格el-table  :data="channelList"  传入数据 type="index" 索引  prop="cate_name" 显示数据     <template #default="{ row }">   自定义数据    row 拿到 一行数据      -->
    <el-table :data="channelList" style="width: 100%" v-loading="loading">
      <el-table-column label="序号" width="100" type="index"> </el-table-column>
      <el-table-column label="分类名称" prop="cate_name"></el-table-column>
      <el-table-column label="分类别名" prop="cate_alias"></el-table-column>
      <el-table-column label="操作" width="100">
        <template #default="{ row }">
          <el-button
            :icon="Edit"
            circle
            plain
            type="primary"
            @click="onEditChannel(row)"
          ></el-button>
          <el-button
            :icon="Delete"
            circle
            plain
            type="danger"
            @click="onDelChannel(row)"
          ></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
  </page-container>
  <channel-edit ref="dialog" @success="onSuccess"></channel-edit>
</template>

<style lang="scss" scoped></style>
